#include "thdmainwindow.h"
#include "ui_thdmainwindow.h"
#include "tdglwidget.h"

#include <QColor>
#include <QColorDialog>

extern struct draw_type DT;
extern struct animation_state AS;
extern struct texture_state TS;
extern struct move_type MT;
extern struct light_type LT;

extern unsigned int SCR_WIDTH;
extern unsigned int SCR_HEIGHT;

ThDMainWindow::ThDMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ThDMainWindow)
{
    ui->setupUi(this);

    // animation disable
    AS.isAnimating = false;

    // no texture
    TS.tex1 = false;
    TS.tex2 = false;
    // color is default to white
    TS.orgColor.setX(1.0f);
    TS.orgColor.setY(1.0f);
    TS.orgColor.setZ(1.0f);

    // move disable
    MT.move_type = 0;
    MT.xoffset = 0.0f;
    MT.yoffset = 0.0f;
    MT.r_xoffset = 0.0f;
    MT.r_yoffset = 0.0f;
    MT.lastX = SCR_WIDTH / 2.0f;
    MT.lastY = SCR_HEIGHT / 2.0f;
    MT.firstMouse = true;

    // default to draw lines
    DT.type = 2;

    // light disable by default
    LT.l_type = 0;
}

ThDMainWindow::~ThDMainWindow()
{
    delete ui;
}

void ThDMainWindow::on_actionstart_triggered()
{
    // animation
    AS.isAnimating = true;
    AS.animation_timer_id = ui->openGLWidget->startTimer(25);
}

void ThDMainWindow::on_actionend_triggered()
{
    ui->openGLWidget->killTimer(AS.animation_timer_id);
    AS.isAnimating = false;
}

void ThDMainWindow::keyPressEvent(QKeyEvent *event) {
    // just pass to openGLWidget
    ui->openGLWidget->keyPressEvent(event);
}

void ThDMainWindow::on_actiontexture1_box_triggered()
{
    // texture1 : box
    TS.tex1 = true;
    TS.tex2 = false;
    ui->openGLWidget->update();
}

void ThDMainWindow::on_actiontexture2_face_triggered()
{
    // texture2 : face
    TS.tex1 = false;
    TS.tex2 = true;
    ui->openGLWidget->update();
}

void ThDMainWindow::on_actionclear_triggered()
{
    // clear all the texture
    TS.tex1 = false;
    TS.tex2 = false;
    ui->openGLWidget->update();
}

void ThDMainWindow::on_actionmix_texture_triggered()
{
    // mix two textures
    TS.tex1 = true;
    TS.tex2 = true;
    ui->openGLWidget->update();
}

void ThDMainWindow::on_actioncolor_triggered()
{
    QColor color = QColorDialog::getColor(Qt::black);
    if (color.isValid()) {
        TS.orgColor.setX(color.redF());
        TS.orgColor.setY(color.greenF());
        TS.orgColor.setZ(color.blueF());
    } else
        return ;
    TS.tex1 = false;
    TS.tex2 = false;
    ui->openGLWidget->update();
}

void ThDMainWindow::on_actionfps_camera_triggered()
{
    // fps camera
    MT.move_type = 1;
    ui->openGLWidget->setCursor(Qt::CrossCursor);   // 十字鼠标
    ui->openGLWidget->initFpsCamera();
}

void ThDMainWindow::on_actionrotation_triggered()
{
    // rotate the object
    MT.move_type = 2;
    ui->openGLWidget->setCursor(Qt::UpArrowCursor);   // 上箭头鼠标
}

void ThDMainWindow::on_actionmove_disable_triggered()
{
    // move disable
    MT.move_type = 0;
    ui->openGLWidget->setCursor(Qt::PointingHandCursor);  // 指向手势鼠标
}

void ThDMainWindow::on_actionpoint_mode_triggered()
{
    // draw points
    DT.type = 1;
    ui->openGLWidget->update();
}

void ThDMainWindow::on_actionline_mode_triggered()
{
    // draw lines
    DT.type = 2;
    ui->openGLWidget->update();
}

void ThDMainWindow::on_actioncover_mode_triggered()
{
    // draw faces
    DT.type = 3;
    ui->openGLWidget->update();
}

void ThDMainWindow::on_actionfixed_point_light_source_triggered()
{
    LT.l_type = 1;
    ui->openGLWidget->update();
}

void ThDMainWindow::on_actiondirectional_light_triggered()
{
    LT.l_type = 2;
    ui->openGLWidget->update();
}


void ThDMainWindow::on_actionspotlight_triggered()
{
    LT.l_type = 3;
    ui->openGLWidget->update();
}

void ThDMainWindow::on_actionno_light_triggered()
{
    LT.l_type = 0;
    ui->openGLWidget->update();
}

void ThDMainWindow::on_actionX_Positive_triggered()
{
    ui->openGLWidget->setXpositive();
}

void ThDMainWindow::on_actionX_Negative_triggered()
{
    ui->openGLWidget->setXnegative();
}

void ThDMainWindow::on_actionY_Positive_triggered()
{
    ui->openGLWidget->setYpositive();
}

void ThDMainWindow::on_actionY_Negative_triggered()
{
    ui->openGLWidget->setYnegative();
}

void ThDMainWindow::on_actionZ_Positive_triggered()
{
    ui->openGLWidget->setZpositive();
}

void ThDMainWindow::on_actionZ_Negative_triggered()
{
    ui->openGLWidget->setZnegative();
}
